package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.bean.AddDeviceBean;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 状态数据列表
 */
public final class AddDeviceCategoryAdapter extends AppAdapter<AddDeviceBean> {

    public AddDeviceCategoryAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvDeviceTypeName;
        private final RecyclerView recyclerView;

        private ViewHolder() {
            super(R.layout.item_add_device_category);
            tvDeviceTypeName = findViewById(R.id.tv_device_type_name);
            recyclerView = findViewById(R.id.recycler_view);
        }

        @Override
        public void onBindView(int position) {
            AddDeviceBean item = getItem(position);
            tvDeviceTypeName.setText(item.getProductClassName());
            AddDeviceAdapter addDeviceAdapter = (AddDeviceAdapter) recyclerView.getAdapter();
            if(addDeviceAdapter == null){
                addDeviceAdapter = new AddDeviceAdapter(getContext());
                recyclerView.setAdapter(addDeviceAdapter);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
                gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
                recyclerView.setLayoutManager(gridLayoutManager);
            }
            addDeviceAdapter.setData(item.getDistributionNetworkMethods());
            addDeviceAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    if(mOnItemClickListener != null){
                        AddDeviceBean.DeviceConfigNetworkBean deviceConfigNetworkBean = ((AddDeviceAdapter)recyclerView.getAdapter()).getItem(position);
                        mOnItemClickListener.onItemClick(deviceConfigNetworkBean);
                    }
                }
            });
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }


    private OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(@Nullable OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(AddDeviceBean.DeviceConfigNetworkBean deviceConfigNetworkBean);
    }
}